package trabajoISW2.domain.implementations;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import trabajoISW2.domain.contracts.ICreditCard;
import trabajoISW2.domain.exceptions.NoPositiveNumberException;
import trabajoISW2.domain.exceptions.WrongNameException;

@Entity
public class CreditCard implements ICreditCard{
	
	@Id
	private String number;
	
	private String type;
	private int cvv, month, year;
	
	@ManyToOne (cascade = {CascadeType.MERGE, CascadeType.REFRESH})
	private User user;
	
	@OneToMany (mappedBy = "card")
	private List<Order> orders;
	
	//Constructor
	public CreditCard(String type, String number, int cvv, int month, int year){
		this.type = type;
		this.number = number;
		this.cvv = cvv;
		this.month = month;
		this.year = year;
	}
	
	public CreditCard(String type, String number, int cvv, int month, int year, User u){
		this.type = type;
		this.number = number;
		this.cvv = cvv;
		this.month = month;
		this.year = year;
		this.user = u;
	}
	
	public CreditCard(){
		super();
	}
	
	
	@Override
	public void setType(String type) {
		if(type == null| type == "")
			throw new WrongNameException();
		else
			this.type = type;
	}

	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return this.type;
	}

	@Override
	public void setNumber(String number) {
		if(number == null| number =="")
			throw new IllegalArgumentException();
		else
			this.number = number;
	}

	@Override
	public String getNumber() {
		
		return this.number;
	}

	@Override
	public void setCvv(int cvv) {
		if(cvv<0|cvv>999)
			throw new NoPositiveNumberException();
		else
			this.cvv = cvv;
	}

	@Override
	public int getCvv() {
		// TODO Auto-generated method stub
		return this.cvv;
	}

	@Override
	public void setMonth(int month) {
		if(month<=0|month>12)
			throw new IllegalArgumentException();
		else	
			this.month = month;
	}

	@Override
	public int getMonth() {
		// TODO Auto-generated method stub
		return this.month;
	}

	@Override
	public void setYear(int year) {
		if(year<2011)
			throw new IllegalArgumentException();
		else
			this.year = year;
	}

	@Override
	public int getYear() {
		// TODO Auto-generated method stub
		return this.year;
	}

	public void setUser(User user) {
		if(user == null)
			throw new IllegalArgumentException();
		else
			this.user = user;
	}

	public User getUser() {
		return user;
	}


	public void setOrders(List<Order> orders) {
		if(orders == null)
			throw new IllegalArgumentException();
		else
			this.orders = orders;
	}

	public List<Order> getOrders() {
		return orders;
	}
	
	public boolean equals (Object o){
		boolean b = false;
		CreditCard cc = (CreditCard) o;
		if (getNumber().equals(cc.getNumber()))
			b = true;
		return b;
	}


}
